import { Switch, type GetProps } from "antd";
import { useState } from "react";

type LazySwitchProps = Omit<GetProps<typeof Switch>, "onChange"> & {
  onChange?: (value: boolean) => Promise<any>;
};
export default function LazySwitch(props: LazySwitchProps) {
  const { defaultChecked, onChange, ...rest } = props;

  const [checked, setChecked] = useState(defaultChecked);

  return (
    <Switch
      checked={checked}
      onChange={(v) => {
        setChecked(v);
        onChange?.(v).catch(() => {
          setChecked(!v);
        });
      }}
      {...rest}
    />
  );
}
